package com.veo.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.veo.entity.Books;
import com.veo.entity.BorrowBooks;
import com.veo.service.BookManageService;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component("booksManageAction")
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
@Results({
        @Result(name = "success",type = "json",params = {"root","resultMapToBooks"}),
        @Result(name = "true",type = "json",params = {"root","resultMapToBorrowBooks"}),
        @Result(name = "ok",type = "json",params = {"root","resultMap"}),
        @Result(name = "getAll",type = "redirect",location = "/borrowBooks.jsp"),
        @Result(name = "getAll1",type = "redirect",location = "/returnBooks.jsp"),
})
public class BooksManageAction extends ActionSupport implements ModelDriven<Books> {

   @Autowired
   private BookManageService bookManageService;

    private List<BorrowBooks> resultMapToBorrowBooks;

    private Map<String,String> resultMap;

    public Map<String, String> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, String> resultMap) {
        this.resultMap = resultMap;
    }

    public List<BorrowBooks> getResultMapToBorrowBooks() {
        return resultMapToBorrowBooks;
    }

    public void setResultMapToBorrowBooks(List<BorrowBooks> resultMapToBorrowBooks) {
        this.resultMapToBorrowBooks = resultMapToBorrowBooks;
    }


    @Action("getAllBorrowBooks")
    public String getBorrowBooks(){
        resultMapToBorrowBooks=bookManageService.getAllBorrowBooks();
        return  "true";
    }



    @Action("findIsbn")
    public String findIsbn(){
        resultMap=new HashMap<String,String>();
        try {
            boolean flag = bookManageService.findIsbn(books);
            if (flag){
                resultMap.put("msg","true");
            }
            else {
                resultMap.put("msg","false");
            }
        }catch (Exception e){
            resultMap.put("msg","error");
            return "ok";
        }
        return "ok";
    }

    @Action("addBorrowBooks")
    public String addBorrowBooks(){
        try {
            String idCard= ServletActionContext.getRequest().getParameter("idCard");
            String isbn=ServletActionContext.getRequest().getParameter("isbn");
            int id = bookManageService.findUserId(idCard).getId();
            int bookId = bookManageService.findBookId(isbn).getId();
            if (bookManageService.findBorrowBooks(String.valueOf(id),String.valueOf(bookId))){
                resultMap.put("msg","ext");
                return "ok";
            }
            bookManageService.addBorrowBooks(String.valueOf(id),String.valueOf(bookId));
        }catch (Exception e){
            return "getAll";
        }
        return "getAll";
    }

    @Action("deleteBorrowBooks")
    public String deleteBorrowBooks(){
        try {
            String idCard= ServletActionContext.getRequest().getParameter("idCard");
            String isbn=ServletActionContext.getRequest().getParameter("isbn");
            int id = bookManageService.findUserId(idCard).getId();
            int bookId = bookManageService.findBookId(isbn).getId();
            bookManageService.deleteBorrowBooks(String.valueOf(id),String.valueOf(bookId));
        }catch (Exception e){
            return "getAll1";
        }
        return "getAll1";
    }


    private Books books=new Books();
    @Override
    public Books getModel() {
        return books;
    }
}
